﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gfw.BLL.Basic.Stock;
using OpenFlashChart;

namespace Gfw.Web.Areas.Stock.Controllers
{
    public class MarketController : Controller
    {
        //
        // GET: /Stock/Market/
        [LAuthorize]
        public ActionResult Index()
        {
            return View();
        }

        [LAuthorize]
        public PartialViewResult Graphic()
        {
            return PartialView();
        }

        [LAuthorize]
        [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
        public string Price()
        {
             var dates = new List<string>();
            var vols = new List<decimal>();
            var prices = new List<decimal>();
            StockMarket.GetTendency(out vols, out prices, out dates);

           

            //var startDate = DateTime.Today.AddMonths(-1);
            //var random = new Random();
            //for (int i = 0; i < 31; i++)
            //{
            //    dates.Add(startDate.AddDays(i).ToShortDateString());
            //    vols.Add(random.Next(200000));
            //    prices.Add(random.Next(100));
            //}

            OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
            chart.X_Axis.Labels.SetLabels(dates);
            chart.X_Axis.Labels.Steps = vols.Count > 20 ? vols.Count/10 : 1;
            chart.X_Axis.Labels.Rotate = "2";

            //构建价格走势

            OpenFlashChart.Line priceline = new Line()
            {
                Values = prices,
                HaloSize = 0,
                Width = 1,
                DotSize = 5
            };

            priceline.DotStyleType.Tip = "价格：#val#<br>日期:#x_label#"; ;
            chart.AddElement(priceline);

            chart.Y_Axis.Min =Convert.ToDouble( prices.Min() - (prices.Max() - prices.Min()) / 2);
            chart.Y_Axis.Max = Convert.ToDouble(prices.Max());
            chart.Y_Axis.Offset = true;
            chart.Y_Axis.Steps = (int)(chart.Y_Axis.Max - chart.Y_Axis.Min) / 10;
            chart.Y_Legend = new Legend("价格") { Style = "{font-size: 14px;; color: black}" };


            //构建成交量
          
            Bar volbar = new Bar()
                             {
                                 Colour = "#344565",
                                 Alpha = 0.8,
                                 FontSize = 10,
                                 Values = vols
                             };
            volbar.Tooltip = "成交：#val#<br>日期:#x_label#";

            volbar.AttachToRightAxis(true);

            chart.AddElement(volbar);
            

            chart.Y_Axis_Right = new YAxis();
           
            //chart.Y_Axis_Right.Min = vols.Min();
            //chart.Y_Axis_Right.Max = (vols.Max() - vols.Min()) * 2 + vols.Max();
            chart.Y_Axis_Right.Min = 0;
            chart.Y_Axis_Right.Max = Convert.ToDouble(vols.Max()  * 3 );
            // volchart.Y_Axis.Steps = (vols.Max() - vols.Max()) / 3;
            //chart.Y_Axis_Right.Offset = true;

            chart.Y_RightLegend = new Legend("成交量") { Style = "{font-size: 14px;; color: black}" }; ;


           

            chart.Title = new Title("走势图");
            chart.Title.Style = " {font-size: 25px; color: #FFB900}";
            return chart.ToString();
            //return Json(volchart.ToString(), JsonRequestBehavior.AllowGet);
        }

        [LAuthorize]
        public ActionResult Bargain()
        {
            var model = StockMarket.GetBargainModel();
            return View(model);
        }
    }
}
